#ifndef __MAIN_CPP__
#define __MAIN_CPP__

#include "json_io.cpp"
#include "lzj_parser.cpp"
#include <iomanip>

int main() {
    std::ifstream grammarIn;
    fstream grammarOut;
    std::ifstream fileIn;
    fstream parseOut;
    fstream astJsonOut;
    fstream astTxtOut;
    OPEN_AND_CHECK(grammarIn, "./syntax_new.txt", false);
    OPEN_AND_CHECK(grammarOut, "./results/parseTable.csv", true);
    OPEN_AND_CHECK(fileIn, "./testfiles/test15_complex.txt", false);
    OPEN_AND_CHECK(parseOut, "./results/parse_tree.txt", true);
    OPEN_AND_CHECK(astJsonOut, "./results/AST.json", true);
    OPEN_AND_CHECK(astTxtOut, "./results/AST.txt", true);
    // ---------------------------------------------------------------------- //
    clock_t st_c = clock();
    createParser(grammarIn);
    clock_t ed_c = clock();
    printParseTable(grammarOut);
    cout << "Parse table saved at ./parseTable.csv\n";
    clock_t st_p = clock();
    PtrParseTreeNode parseTree = lzjParser(fileIn, lzjlex(fileIn, fstream()));
    clock_t ed_p = clock();
    printParseTree(parseOut, parseTree);
    cout << "Parse tree saved at ./results/parse_tree.txt\n";
    initializeASTNodeWithJson("./convertion.jsonc");
    PtrASTNode ast = ASTNode::buildAST(parseTree);
    printAST(astTxtOut, ast);
    astJsonOut << std::setw(2) << json_wrap(ast) << endl;
    cout << "AST saved at /results/AST.txt\n";
    cout << "Creation of parser used " << ed_c - st_c << "milliseconds\n";
    cout << "Parsing used " << ed_p - st_p << " milliseconds\n";
    // ---------------------------------------------------------------------- //
    grammarOut.close();
    grammarIn.close();
    fileIn.close();
    parseOut.close();
    astJsonOut.close();
    astTxtOut.close();
}

#endif